package com.liuxing.javaSpringBoot.modules.account.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuxing.javaSpringBoot.modules.account.dao.RoleDao;
import com.liuxing.javaSpringBoot.modules.account.entity.Role;
import com.liuxing.javaSpringBoot.modules.account.service.RoleService;
import com.liuxing.javaSpringBoot.modules.common.entity.Result;
import com.liuxing.javaSpringBoot.modules.common.entity.SearchBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * Created by IntelliJ IDEA.
 * @File RoleServiceImpl.java
 * @Auth liuxing
 * @Date 2021/8/23 16:46
 * @Email liuxing997@foxmail.com
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    //添加角色信息
    @Override
    @Transactional
    public Result<Role> insertRole(Role role) {
        role.setCreateDate(LocalDateTime.now());
        roleDao.insertRole(role);
        return new Result<Role>(Result.ResultCode.SUCCESS.code,
                "Insert success.",role);
    }

    //根据角色ID修改角色信息
    @Override
    @Transactional
    public Result<Role> updateRoleById(Role role) {
        roleDao.updateRoleById(role);
        return new Result<Role>(Result.ResultCode.SUCCESS.code,
                "Update success.",role);
    }

    //根据角色ID删除角色
    @Override
    @Transactional
    public Result<Object> deleteRoleByRoleId(int id) {
        roleDao.deleteRoleByRoleId(id);
        return new Result<>(Result.ResultCode.SUCCESS.code,
                "Delete success.");
    }

    //根据角色ID查询角色
    @Override
    public Role getRoleByRoleId(int id) {
        return roleDao.getRoleByRoleId(id);
    }

    //根据用户ID查询角色信息
    @Override
    public List<Role>  getRolesByUserId(int userId) {
        List<Role> roles =  Optional.
                ofNullable(roleDao.getRolesByUserId(userId))
                .orElse(Collections.emptyList());
        return roles;
    }

    //查询所有角色信息
    @Override
    public List<Role> getAllRole() {
        return Optional
                .ofNullable(roleDao.getAllRole())
                .orElse(Collections.emptyList());
    }

    //批量插入角色信息
    @Override
    @Transactional
    public Result<Object> insertRoles(List<Role> roles) {
        roles.stream().forEach(item ->{
            item.setCreateDate(LocalDateTime.now());
        });
        roleDao.insertRoles(roles);
        return new Result<>(Result.ResultCode.SUCCESS.code,
                "Insert Roles success.");
    }

    //分页查询角色信息
    @Override
    public PageInfo<Role> getRolesBySearchBean(SearchBean searchBean) {
        searchBean.initSearchBean();
        PageHelper.startPage(searchBean.getCurrentPage(),searchBean.getPageSize());
        return new  PageInfo<> (Optional
                .ofNullable(roleDao.getRolesBySearchBean(searchBean))
                .orElse(Collections.emptyList()));
    }
}
